//页面初始化
$(function () {
    //加载数据
    queryRoomInfo();
})

//查询房间信息列表
function  queryRoomInfo() {
    //查询楼层信息
    var floorNo = $("#floorNo").val();
    //左侧菜单栏
    layui.use(['table'], function() {
        var table = layui.table;
        //第一个实例
        table.render({
            elem: '#test'
            ,url: getRootPath_web()+'baseData/room/obtainRoomData',
            method:'post',
            where:{floorNo:floorNo},
            parseData: function(res){
                return {
                    "code": res.code, //解析接口状态
                    "msg": res.codeDesc, //解析提示文本
                    "count": res.roomCount, //解析数据长度
                    "data": res.data //解析数据列表
                };
            },
            page: false //开启分页
            ,
            cols: [
                [ //表头
                    {
                        field: 'roomName',
                        title: '房间号',
                        fixed: 'left'
                    }, {
                    field: 'totalBed',
                    title: '总床位数（个）',
                }, {
                    field: 'emptyBed',
                    title: '空床位数（个）',
                }, {
                    field: 'useBed',
                    title: '已入住人数（个）',
                }, {
                    field: 'remark',
                    title: '用途',
                    fixed: 'right',
                }, {
                    field: 'operation',
                    title: '操作',
                    fixed: 'right',
                    style: 'color:#1C9399;',
                    fixed: 'right',
                    width: 250,
                    templet: function(res){
                        console.log(res);
                        return "<div class='table-list-btn'>" +
                            "<span class='cursor' lay-event=\"detail\">床位管理</span>|"+
                            "<span class='cursor' lay-event=\"edit\">编辑</span>|" +
                            "<span class='red cursor' lay-event=\"del\">删除</span>" +
                            "</div>"
                    }
                }
                ]
            ]
        });
        table.on('tool(test)', function(obj){
            var data = obj.data;
            if(obj.event === 'del'){
                var roomNo = data.roomNo;
                deleteRoom(roomNo,obj);
            } else if(obj.event === 'edit'){
                var roomNo = data.roomNo;
                updateRoom(roomNo);
            }else if(obj.event ==='detail'){
                var roomNo = data.roomNo;
                addBedInfo(roomNo);
            }
        });
    });
}

//房间添加
function addRoomInfo() {
    var floorNo = $("#floorNo").val();
    var str="<div class=\"pop-up\">";
    str+="<div>";
    str+="<div class=\"pop-title relative overflow mb15px\">";
    str+="<span class=\"ml15px\">新建房间</span>";
    str+="<p class=\"right closeBtn\" style='cursor: pointer'><img src='"+getRootPath_web()+"static/img/close_icon.png' class=\"block pop-close\"><p>";
    str+="</div>";
    str+="<form id='buildingForm'>";
    str+="<input name='floorNo' type='hidden' value='"+floorNo+"'>";
    str+="<div class=\"pop-form mt30px mb30px\">";
    str+="<div class=\"layui-form-item\">";
    str+="<label class=\"layui-form-label\"><span class=\"red\">*</span>房间名称</label>";
    str+="<div class=\"layui-input-block\">";
    str+="<input type=\"text\" name='roomName' maxlength='10' placeholder=\"房间名称\" class=\"layui-input roomName\">";
    str+="</div>";
    str+="</div>";
    str+="<div class=\"layui-form-item\">";
    str+="<label class=\"layui-form-label\"><span class=\"red\">*</span>用途</label>";
    str+="<div class=\"layui-input-block layui-input-block-two overflow\">";
    str+="<p class=\"left\">";
    str+="<select name='roomType' class=\"layui-input form-input-pr roomType\">";
        str+="<option value =\"101\">空床位</option>";
        str+="<option value =\"102\">其他</option>";
    str+="</select>";
    str+="</p>";
    str+="<p class=\"right\">";
        str+="<input type=\"text\" name='roomRemark' style='display: none' maxlength='8' placeholder=\"其他用途\" class=\"layui-input roomRemark\">";
    str+="</p>";
    str+="</div>";
    str+="</div>";
    str+="</div>";
    str+="<div class=\"btn-con textCenter\">";
    str+="<span class=\"btn-two btn-submit cursor\">提&nbsp;交</span>";
    str+="</div>";
    str+="</div>";
    str+="</form>";
    str+="</div>";
    layui.use(['layer'], function() {
        var layer = layui.layer;
        layer.open({
            type: 1,
            title: false, //不显示标题栏
            closeBtn: false,
            area: ['500px', ''],
            shade: 0.6,
            id: 'payCons', //设定一个id，防止重复弹出
            /*,btn: ['确认支付']*/
            btnAlign: 'c',
            moveType: 1, //拖拽模式，0或者1
            shadeClose: false, //设置允许点遮罩关闭
            content: str,
            success: function() {
                //关闭弹层
                $(".closeBtn").click(function() {
                    layer.closeAll();
                });
                $(".btn-submit").click(function () {
                    saveRoomInfo();
                })
                //更换床位用途
                $(".roomType").on("change",function () {
                    var roomUser = $(this).val();
                    if(roomUser==101){
                        $(".roomRemark").css("display","none");
                    }else {
                        $(".roomRemark").css("display","block");
                    }
                })
            },
        });
    });
}
//保存房间信息
function saveRoomInfo() {
    var floorNo =$("#floorNo").val();
    var buildingForm = new FormData(document.getElementById("buildingForm"));
    layui.use('layer', function(){
        var layer = layui.layer;
        //房间名称
        var roomName = $(".roomName").val();
        if(roomName==''){
            layer.msg('房间名称不能为空！');
            return false;
        }
        //房间用途
        var roomType = $(".roomType").val();
        if(roomType=='102'){
            //其他用途
            var remark = $(".roomRemark").val();
            if(remark==''){
                layer.msg('请输入房间用途！');
                return false;
            }
        }
        var loading;
        // layer.confirm('确定添加该房间吗？', {
        //     btn: ['确定','取消'] //按钮
        // }, function(){
        //
        // }, function(){
        //     layer.close(layer.index);
        // });
        $.ajax({
            url:getRootPath_web()
                +'baseData/room/saveRoomInfo',
            type:'post',
            dataType:'json',
            data:buildingForm,
            processData: false,
            contentType: false,
            beforeSend:function () {
                loading = layer.load(1, {
                    shade: [0.9,'#fff']
                    ,shadeClose:false
                });
            },
            success:function (result) {
                if(result.code=='0000'){
                    layer.confirm('添加成功', {
                        btn: ['确定'] //按钮
                        ,shadeClose: false
                        ,closeBtn:false
                    }, function(){
                        layer.closeAll();
                        window.location.href=getRootPath_web()
                            +"baseData/room/roomList?floorNo="+floorNo;
                    })
                }else {
                    layer.confirm(result.codeDesc, {
                        btn: ['确定'] //按钮
                        ,shadeClose: false
                        ,closeBtn:false
                    }, function(){
                        layer.close(layer.index);
                        layer.close(loading);
                    })
                }
            }
        })
    });
}

//修改楼苑信息
function updateRoom(roomNo){
    $.ajax({
        url:getRootPath_web()+'baseData/room/queryRoomInfo',
        type:'post',
        dataType:'json',
        data:{"roomNo":roomNo},
        success(result){
            var str="<div class=\"pop-up\">";
            str+="<div>";
            str+="<div class=\"pop-title relative overflow mb15px\">";
            str+="<span class=\"ml15px\">新建房间</span>";
            str+="<p class=\"right closeBtn\" style='cursor: pointer'><img src='"+getRootPath_web()+"static/img/close_icon.png' class=\"block pop-close\"><p>";
            str+="</div>";
            str+="<form id='buildingForm'>";
            str+="<div class=\"pop-form mt30px mb30px\">";
            str+="<div class=\"layui-form-item\">";
            str+="<label class=\"layui-form-label\"><span class=\"red\">*</span>房间名称</label>";
            str+="<input name='roomNo' type='hidden' value='"+result.roomNo+"' />";
            str+="<div class=\"layui-input-block\">";
            str+="<input type=\"text\" name='roomName' value='"+result.name+"' maxlength='10' placeholder=\"房间名称\" class=\"layui-input roomName\">";
            str+="</div>";
            str+="</div>";
            str+="<div class=\"layui-form-item\">";
            str+="<label class=\"layui-form-label\"><span class=\"red\">*</span>用途</label>";
            str+="<div class=\"layui-input-block layui-input-block-two overflow\">";
            str+="<p class=\"left\">";
            if(result.uploadFlag==0){
                str+="<select name='roomType' readonly class=\"layui-input form-input-pr roomType\">";
            }else {
                str+="<select name='roomType' class=\"layui-input form-input-pr roomType\">";
            }
            if(result.type==101){
                str+="<option selected value =\"101\">空床位</option>";
            }else {
                str+="<option value =\"101\">空床位</option>";
            }
            if(result.type==102){
                str+="<option selected value =\"102\">其他</option>";
            }else {
                str+="<option value =\"102\">其他</option>";
            }
            str+="</select>";
            str+="</p>";
            str+="<p class=\"right\">";
            if(result.type==101){
                str+="<input type=\"text\" style='display: none' name='roomRemark' value='"+result.remark+"' maxlength='8' placeholder=\"其他用途\" class=\"layui-input roomRemark\">";
            }else {
                str+="<input type=\"text\" name='roomRemark' value='"+result.remark+"' maxlength='8' placeholder=\"其他用途\" class=\"layui-input roomRemark\">";
            }
            str+="</p>";
            str+="</div>";
            str+="</div>";
            str+="</div>";
            str+="<div class=\"btn-con textCenter\">";
            str+="<span class=\"btn-two btn-submit cursor\">修&nbsp;改</span>";
            str+="</div>";
            str+="</div>";
            str+="</form>";
            str+="</div>";
            layui.use(['layer'], function() {
                var layer = layui.layer;
                layer.open({
                    type: 1,
                    title: false, //不显示标题栏
                    closeBtn: false,
                    area: ['500px', ''],
                    shade: 0.6,
                    id: 'payCons', //设定一个id，防止重复弹出
                    /*,btn: ['确认支付']*/
                    btnAlign: 'c',
                    moveType: 1, //拖拽模式，0或者1
                    shadeClose: false, //设置允许点遮罩关闭
                    content: str,
                    success: function() {
                        //关闭弹层
                        $(".closeBtn").click(function() {
                            layer.closeAll();
                        });
                        $(".btn-submit").click(function () {
                            updateRoomInfo();
                        })
                        //更换床位用途
                        $(".roomType").on("change",function () {
                            var roomUser = $(this).val();
                            if(roomUser==101){
                                $(".roomRemark").css("display","none");
                            }else {
                                $(".roomRemark").val("");
                                $(".roomRemark").css("display","block");
                            }
                        })
                    },
                });
            });
        }
    })

}
//修改楼苑信息
function updateRoomInfo() {
    var buildingForm = new FormData(document.getElementById("buildingForm"));
    layui.use('layer', function(){
        var layer = layui.layer;
        //房间名称
        var roomName = $(".roomName").val();
        if(roomName==''){
            layer.msg('房间名称不能为空！');
            return false;
        }
        //房间用途
        var roomType = $(".roomType").val();
        if(roomType=='102'){
            //其他用途
            var remark = $(".roomRemark").val();
            if(remark==''){
                layer.msg('请输入房间用途！');
                return false;
            }
        }
        var loading;
        // layer.confirm('确定修改该房间吗？', {
        //     btn: ['确定','取消'] //按钮
        // }, function(){
        //
        // }, function(){
        //     layer.close(layer.index);
        // });
        $.ajax({
            url:getRootPath_web()
                +'baseData/room/updateRoomInfo',
            type:'post',
            dataType:'json',
            data:buildingForm,
            processData: false,
            contentType: false,
            beforeSend:function () {
                loading = layer.load(1, {
                    shade: [0.9,'#fff']
                    ,shadeClose:false
                });
            },
            success:function (result) {
                if(result.code=='0000'){
                    layer.confirm('修改成功', {
                        btn: ['确定'] //按钮
                        ,shadeClose: false
                        ,closeBtn:false
                    }, function(){
                        layer.closeAll();
                        window.location.href=getRootPath_web()
                            +"baseData/room/roomList?floorNo="+result.floorNo;
                    })
                }else {
                    layer.confirm(result.codeDesc, {
                        btn: ['确定'] //按钮
                        ,shadeClose: false
                        ,closeBtn:false
                    }, function(){
                        layer.close(layer.index);
                        layer.close(loading);
                    })
                }
            }
        })
    });
}
//删除楼苑信息
function deleteRoom(roomNo,obj) {
    layui.use('layer', function(){
        var layer = layui.layer;
        layer.confirm('确定删除该房间吗？', {
            btn: ['确定','取消'] //按钮
        }, function(){
            $.ajax({
                url:getRootPath_web()
                    +'baseData/room/deleteRoomInfo',
                type:'post',
                dataType:'json',
                data:{"roomNo":roomNo},
                success:function (result) {
                    if(result.code=='0000'){
                        layer.confirm('删除成功', {
                            btn: ['确定'] //按钮
                            ,shadeClose: false
                        }, function(){
                            obj.del();
                            layer.closeAll();
                        })
                    }else {
                        layer.confirm(result.codeDesc, {
                            btn: ['确定'] //按钮
                            ,shadeClose: false
                        }, function(){
                            layer.close(layer.index);
                        })
                    }
                }
            })
        }, function(){
            layer.close(layer.index);
        });
    });
}
//返回楼层列表
function returnAhead() {
    var buildingNo= $("#buildingNo").val();
    window.location.href=getRootPath_web()+"baseData/floor/floorList?buildingNo="+buildingNo;
}